package com.ccbooks.listener;

import org.w3c.dom.Text;

import com.ccbooks.bo.BookMarkBo;
import com.ccbooks.bo.FileBo;
import com.ccbooks.util.BooksEngine;
import com.ccbooks.util.ContentDisp;
import com.ccbooks.util.ContentUtil;
import com.ccbooks.util.ContentUtilMax;
import com.ccbooks.util.ContentUtilReturn;
import com.ccbooks.util.CursorController;
import com.ccbooks.util.StringConfig;
import com.ccbooks.util.TextSizeUtil;
import com.ccbooks.util.PDB.ContentDispPDB;
import com.ccbooks.util.PDB.ContentUtilMaxPDB;
import com.ccbooks.util.PDB.ContentUtilPDB;
import com.ccbooks.util.PDB.ContentUtilReturnPDB;
import com.ccbooks.view.BookContentView;
import com.ccbooks.view.R;

import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;

public class SbrMainContentOnChangeListener implements OnSeekBarChangeListener {

	private BookContentView bcv;
	private ContentUtil cu;
	private ContentUtilPDB cuPDB;
	private ContentUtilMaxPDB cumPDB;
	private ContentUtilMax cum;
	public SbrMainContentOnChangeListener(BookContentView bcv){
		this.bcv = bcv;
	}
	
	@Override
	public void onProgressChanged(SeekBar seekBar, int progress,
			boolean fromUser) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onStartTrackingTouch(SeekBar seekBar) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onStopTrackingTouch(SeekBar seekBar) {
		// TODO Auto-generated method stub
		bcv.dg.show();
		seekBar.setMax(100);
		if(FileBo.BOOKTYPE_TXT.equals(bcv.book.booktype)){
		if (bcv.portraint == 1) {
			
			int percent = seekBar.getProgress(); 
			
			int fontSize  = TextSizeUtil.fontSize;
			TextSizeUtil.fontSize=fontSize;
			
			
			bcv.lineCount = TextSizeUtil.getLineCount(fontSize, bcv.portraint);
			if(percent == 100){
				cum = new ContentUtilMax(bcv,bcv.book.bookpath,"宋体",StringConfig.VWIDE,"GBK",bcv.portraint,bcv.cu.engine);
				cum.what = 0;
				new Thread(cum).start();
				
				bcv.cd = new ContentDisp(bcv, bcv.book.bookpath, "宋体",StringConfig.VWIDE,
						"GBK", bcv.portraint,bcv.cu.engine, bcv.portraint);
				bcv.cd.setLine(0, bcv.lineCount);
				bcv.cd.what = 1;
				Thread th= new Thread(bcv.cd);
				th.start();
				
			}
			if(percent < 100){
				cu = new ContentUtil(bcv,bcv.book.bookpath,"宋体",StringConfig.VWIDE,"GBK",(short)1,percent,bcv.portraint);
				System.out.println("tvMainContent.getWidth()"+bcv.tvMainContent.getWidth());
				bcv.lineIndex= 0;
				
				//lineCount = TextSizeUtil.getLineCount(TextSizeUtil.fontSize, 1);
				
				
				cu.what = 0;
				new Thread(cu).start();
				
				bcv.cd = new ContentDisp(bcv, bcv.book.bookpath, "宋体",StringConfig.VWIDE,
						"GBK", bcv.portraint,cu.engine, bcv.portraint);
				bcv.cd.setLine(0, bcv.lineCount);
				bcv.cd.what = 1;
				Thread th= new Thread(bcv.cd);
				th.start();
				bcv.cur = new ContentUtilReturn(bcv, bcv.book.bookpath, "宋体", StringConfig.VWIDE, "GBK",bcv. portraint,bcv.cu.engine);
				bcv.cc = new CursorController(cu.engine,cu,bcv. cd, bcv.cur, bcv,bcv.cuf);
			}
			
			BookMarkBo.isMark(bcv);
			if(bcv.mark!=null){
				bcv.btnBookMark.setBackgroundResource(R.drawable.content_btn_onmark);
			}else{
				bcv.btnBookMark.setBackgroundResource(R.drawable.content_btn_outmark);
			}
			System.out.println("contentThread---------->end");
			
		} else if (bcv.portraint == 2) {
			int percent = seekBar.getProgress(); 
			int fontSize  = TextSizeUtil.fontSize;
			TextSizeUtil.fontSize=fontSize;
			bcv.lineCount = TextSizeUtil.getLineCount(fontSize, bcv.portraint);
			if(percent == 100){
				cum = new ContentUtilMax(bcv,bcv.book.bookpath,"宋体",StringConfig.HWIDE,"GBK",bcv.portraint,bcv.cu.engine);
				cum.what = 2;
				new Thread(cum).start();
				
				bcv.cd = new ContentDisp(bcv, bcv.book.bookpath, "宋体",StringConfig.HWIDE,
						"GBK", bcv.portraint,bcv.cu.engine, bcv.portraint);
				bcv.cd.setLine(0, bcv.lineCount);
				bcv.cd.what = 2;
				Thread th= new Thread(bcv.cd);
				th.start();
				
			}
			if(percent <100){
				cu = new ContentUtil(bcv, bcv.book.bookpath, "宋体", StringConfig.HWIDE,
						"GBK", bcv.portraint,percent,bcv.portraint);
				bcv.lineIndex= 0;
				
				cu.setLine(0, bcv.lineCount);
				cu.what = 2;
				new Thread(cu).start();
				
				bcv.cd = new ContentDisp(bcv, bcv.book.bookpath, "宋体",StringConfig.HWIDE,
						"GBK", bcv.portraint,cu.engine, bcv.portraint);
				bcv.cd.setLine(bcv.lineIndex, bcv.lineCount);
				bcv.cd.what = 2;
				Thread th= new Thread(bcv.cd);
				th.start();
				bcv.cur = new ContentUtilReturn(bcv, bcv.book.bookpath, "宋体", StringConfig.HWIDE, "GBK",bcv. portraint,bcv.cu.engine);
				bcv.cc = new CursorController(cu.engine,cu,bcv. cd, bcv.cur,  bcv,bcv.cuf);
			}
			BookMarkBo.isMark(bcv);
			if(bcv.mark!=null){
				bcv.btnBookMark.setBackgroundResource(R.drawable.content_btn_onmark);
			}else{
				bcv.btnBookMark.setBackgroundResource(R.drawable.content_btn_outmark);
			}
			System.out.println("contentThread---------->end");
		}
		}else if(FileBo.BOOKTYPE_PDB.equals(bcv.book.booktype)){
			if (bcv.portraint == 1) {
				
				int percent = seekBar.getProgress(); 
				int fontSize  = TextSizeUtil.fontSize;
				TextSizeUtil.fontSize=fontSize;
				bcv.lineCount = TextSizeUtil.getLineCount(fontSize, bcv.portraint);
				if(percent == 100){
					cumPDB = new ContentUtilMaxPDB(bcv,bcv.book.bookpath,"宋体",StringConfig.VWIDE,"GBK",bcv.portraint,bcv.cuPDB.engine);
					cumPDB.what = 4;
					new Thread(cumPDB).start();
					
					bcv.cdPDB = new ContentDispPDB(bcv, bcv.book.bookpath, "宋体",StringConfig.VWIDE,
							"GBK", bcv.portraint,bcv.cuPDB.engine, bcv.portraint);
					bcv.cdPDB.setLine(0, bcv.lineCount);
					bcv.cdPDB.what = 4;
					Thread th= new Thread(bcv.cdPDB);
					th.start();
					
				}
				if(percent < 100){
					cuPDB = new ContentUtilPDB(bcv,bcv.book.bookpath,"宋体",StringConfig.VWIDE,"GBK",(short)1,percent,bcv.portraint);
					System.out.println("tvMainContent.getWidth()"+bcv.tvMainContent.getWidth());
					bcv.lineIndex= 0;
					
					//lineCount = TextSizeUtil.getLineCount(TextSizeUtil.fontSize, 1);
					
					
					cuPDB.what = 4;
					new Thread(cuPDB).start();
					
					bcv.cdPDB = new ContentDispPDB(bcv, bcv.book.bookpath, "宋体",StringConfig.VWIDE,
							"GBK", bcv.portraint,cuPDB.engine, bcv.portraint);
					bcv.cdPDB.setLine(0, bcv.lineCount);
					bcv.cdPDB.what = 4;
					Thread th= new Thread(bcv.cdPDB);
					th.start();
					//bcv.curPDB = new ContentUtilReturn(bcv, bcv.book.bookpath, "宋体", StringConfig.VWIDE, "GBK",bcv. portraint,bcv.cu.engine);
					bcv.cc = new CursorController(cuPDB.engine,cuPDB,bcv. cdPDB,  bcv,bcv.cufPDB);
				}
				BookMarkBo.isMark(bcv);
				if(bcv.mark!=null){
					bcv.btnBookMark.setBackgroundResource(R.drawable.content_btn_onmark);
				}else{
					bcv.btnBookMark.setBackgroundResource(R.drawable.content_btn_outmark);
				}
				System.out.println("contentThread---------->end");
				
			} else if (bcv.portraint == 2) {
				int percent = seekBar.getProgress(); 
				int fontSize  = TextSizeUtil.fontSize;
				TextSizeUtil.fontSize=fontSize;
				bcv.lineCount = TextSizeUtil.getLineCount(fontSize, bcv.portraint);
				if(percent == 100){
					cumPDB = new ContentUtilMaxPDB(bcv,bcv.book.bookpath,"宋体",StringConfig.HWIDE,"GBK",bcv.portraint,bcv.cuPDB.engine);
					cumPDB.what = 7;
					new Thread(cumPDB).start();
					
					bcv.cdPDB = new ContentDispPDB(bcv, bcv.book.bookpath, "宋体",StringConfig.HWIDE,
							"GBK", bcv.portraint,bcv.cuPDB.engine, bcv.portraint);
					bcv.cdPDB.setLine(0, bcv.lineCount);
					bcv.cdPDB.what = 7;
					Thread th= new Thread(bcv.cdPDB);
					th.start();
					
				}
				if(percent < 100){
					cuPDB = new ContentUtilPDB(bcv, bcv.book.bookpath, "宋体", StringConfig.HWIDE,
							"GBK", bcv.portraint,percent,bcv.portraint);
					bcv.lineIndex= 0;
					
					cuPDB.setLine(0, bcv.lineCount);
					cuPDB.what = 7;
					new Thread(cuPDB).start();
					
					bcv.cdPDB = new ContentDispPDB(bcv, bcv.book.bookpath, "宋体",StringConfig.HWIDE,
							"GBK", bcv.portraint,cuPDB.engine, bcv.portraint);
					bcv.cdPDB.setLine(bcv.lineIndex, bcv.lineCount);
					bcv.cdPDB.what = 7;
					Thread th= new Thread(bcv.cdPDB);
					th.start();
					bcv.curPDB = new ContentUtilReturnPDB(bcv, bcv.book.bookpath, "宋体", StringConfig.HWIDE, "GBK",bcv. portraint,bcv.cuPDB.engine);
					bcv.cc = new CursorController(cuPDB.engine,cuPDB,bcv. cdPDB,  bcv,bcv.cufPDB);
				}	
				BookMarkBo.isMark(bcv);
				if(bcv.mark!=null){
					bcv.btnBookMark.setBackgroundResource(R.drawable.content_btn_onmark);
				}else{
					bcv.btnBookMark.setBackgroundResource(R.drawable.content_btn_outmark);
				}
				System.out.println("contentThread---------->end");
			}
		}
	}

}
